package com.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author haoya_Coding_one
 * @version 1.0
 * @create 2018/12/19 18:48
 * @description TODO 时间处理工具类
 */
public class DateUtil {
    private static Logger logger= LoggerFactory.getLogger(DateUtil.class);
    public static final DateFormat formatStandard=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final DateFormat formatAllNumber=new SimpleDateFormat("yyyyMMddHHmmssSSS");
    public static final DateFormat formatSimpleAllNumber=new SimpleDateFormat("yyyyMMddHHmmss");
    public static final DateFormat formatSimpleNumber=new SimpleDateFormat("yyyyMMdd");
    public static final DateFormat formatOnlyDate=new SimpleDateFormat("yyyy-MM-dd");
    public static final DateFormat week3EnFormat = new SimpleDateFormat("E", Locale.ENGLISH);

    public static String formatCurrentTimeStandard(){
        return formatStandard.format(new Date());
    }

    public static String formatStandard(Date date){
        return null==date?"null":formatStandard.format(date);
    }
    public static Date parseStandard(String str){
        try {
            return StringUtils.isBlank(str)?null:formatStandard.parse(str);
        } catch (ParseException e) {
            logger.error("date parse error, source=["+str+"], error message see:\n"+e.getMessage());
        }
        return null;
    }
    public static String formatAllNumber(Date date){
        return null==date?"null":formatAllNumber.format(date);
    }

    public static String formatSimpleNumber(Date date){
        return null==date?"null":formatSimpleNumber.format(date);
    }

    public static Date parseSimpleAllNumber(String str){
        try {
            return StringUtils.isBlank(str)?null:formatSimpleAllNumber.parse(str);
        } catch (ParseException e) {
            logger.error("date parse error, source=["+str+"], error message see:\n"+e.getMessage());
        }
        return null;
    }
    public static String formatSimpleAllNumber(Date date){
        return null==date?"null":formatSimpleAllNumber.format(date);
    }

    public static Date parseOnlyDate(String str){
        try {
            return StringUtils.isBlank(str)?null:formatOnlyDate.parse(str);
        } catch (ParseException e) {
            logger.error("date parse error, source=["+str+"], error message see:\n"+e.getMessage());
        }
        return null;
    }
    public static String formatOnlyDate(Date date){
        return date==null?null:formatOnlyDate.format(date);
    }

    public static Date addHour(Date date, int step){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, step);
        return calendar.getTime();
    }

    public static Date addSecond(Date date, int step){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, step);
        return calendar.getTime();
    }

    public static Date addDay(Date date, int step){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, step);
        return calendar.getTime();
    }

    public static Date getZeroOfSomeDay(Date baseDate, int offset){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(baseDate);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, offset);
        return calendar.getTime();
    }

    public static Date getFirstDayOfMonth(Date baseDate){
        if (baseDate == null){
            baseDate = new Date();
        }
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(baseDate);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    public static Integer diffDays(Date latter, Date former){
        if (latter == null || former == null){
            return null;
        }
        int divisor = 86400;    //60*60*24
        return (int)((latter.getTime() - former.getTime())%divisor);
    }

    public static String getWeek3En(Date date){
        return date==null?null:week3EnFormat.format(date).toLowerCase();
    }
}
